#include <iostream>
#include <ctype.h>
#include <malloc.h>

using namespace std;
int LIMIT_NUM = 5;

char* str2char(string str){
	char* ch = (char *)malloc(str.size()+1 * sizeof(char));
	return strcpy(ch, str.c_str());
}


bool isNumber(string cadenaStr){
	char* cadena = str2char(cadenaStr);
	for (int i = 0; cadena[i]; ++i)
	{
		if (!isdigit(cadena[i]))
		{
			return false;
		}
	}
	return true;
}

int factorial(int numero){
	if(numero == 0){
		return 1;
	}
	int x = 1;
	int factorial = 1;
	while(x <= numero)
	{
		factorial = factorial * x++;
	}
	return factorial;
}

int calculateDecimal( string acm){  
	int acmNumber = atoi(acm.c_str());
	int posicion = 1;
	int acmResp = 0;
	int numeroAux = 0;
	for(posicion = 1; acmNumber > 0; posicion ++){
		numeroAux = acmNumber % 10;
		acmNumber = acmNumber / 10;
		acmResp = acmResp + (numeroAux * factorial(posicion));
	}

	return acmResp;
}

int ACM(string acmnum){
	bool valido = true;

	if(acmnum == ""){
		valido = false;
		cout << "Cadena Vacia, ingrese datos validos"<<endl;
	}
	if(acmnum.size()>LIMIT_NUM){
		valido = false;
		cout << "Es mayor al largo permitido"<<endl;
	}
	if(!isNumber(acmnum)){
		valido = false;
		cout << "No es un numero, ingrese datos validos"<<endl;
	}

	if(valido){
		return calculateDecimal(acmnum) ;
	}
}

int main (int argc, char *argv[])
{
	string acmnum ;
	while(true){
		cout << "ACM de :";
		cin >> acmnum;
		if(acmnum == "0") return 0;
		cout << ACM(acmnum) << endl;
		cin.get();
	}
	return 0;
}
